﻿package edu.pku.se.chocolate.UI.View;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.R;

public class MainSettingsView extends ChocolateView {

	private CheckBox[] visible = new CheckBox[10]; 
	private Spinner options;
	public MainSettingsView(MainView cmv,View self) {
		super(self);
		final MainView mv = cmv;
		//设置多选列表
		visible[0] = (CheckBox) self.findViewById(R.id.checkBox_ac_1);
		visible[1] = (CheckBox) self.findViewById(R.id.checkBox_ac_2);
		visible[2] = (CheckBox) self.findViewById(R.id.checkBox_ac_3);
		visible[3] = (CheckBox) self.findViewById(R.id.checkBox_ac_4);
		visible[4] = (CheckBox) self.findViewById(R.id.checkBox_ac_5);
		visible[5] = (CheckBox) self.findViewById(R.id.checkBox_ac_6);
		visible[6] = (CheckBox) self.findViewById(R.id.checkBox_ac_7);
		visible[7] = (CheckBox) self.findViewById(R.id.checkBox_ac_8);
		visible[8] = (CheckBox) self.findViewById(R.id.checkBox_ac_9);
		visible[9] = (CheckBox) self.findViewById(R.id.checkBox_ac_10);
		for(int i=0;i<10;i++) {
			visible[i].setChecked(mv.getAdapter().isVisible(i));
			final int p = i;
			visible[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton arg0,boolean arg1) {
					if(visible[p].isChecked()) {
						mv.getAdapter().choose(p);
					}
					else {
						mv.getAdapter().unchoose(p);
					}
				}
			});
		}
		
		//设置下拉列表
		final List<String> list = new ArrayList<String>();
		list.add("1分钟");
		list.add("5分钟");
		list.add("15分钟");
		list.add("30分钟");
		list.add("45分钟");
		list.add("1小时");
		list.add("2小时");
		list.add("6小时");
		list.add("12小时");
		list.add("1天");
		list.add("从不");
		final int[] timeMap = {60,300,900,1800,2700,3600,7200,21600,43200,86400,0};
		options = (Spinner) self.findViewById(R.id.spinner1);
		ArrayAdapter<String> sa = new ArrayAdapter<String>(ChocolateView.mainActivity,
				android.R.layout.simple_spinner_item, list);
		sa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		options.setAdapter(sa);
		int frequency = ChocolateActivity.c_solver.getRefleshFrequency();
		for(int i=0;i<timeMap.length;i++) {
			if(timeMap[i] == frequency)
			options.setSelection(i);
		}
		
		options.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				String selected = arg0.getItemAtPosition(arg2).toString();
				for(int i=0;i<list.size();i++) {
					if(list.get(i).equals(selected))
						ChocolateActivity.c_solver.setRefleshFrequency(timeMap[i]);//做该做的事
				}
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
			
		});
		
		//add by jht
		final List<String> slist = new ArrayList<String>();
		slist.add("软工正式服务器");
		slist.add("校内临时服务器");
		Spinner server = (Spinner) self.findViewById(R.id.spinner2);
		ArrayAdapter<String> server_a = new ArrayAdapter<String>(ChocolateView.mainActivity,
				android.R.layout.simple_spinner_item, slist);
		server_a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		server.setAdapter(server_a);
		int serid = ChocolateActivity.c_solver.getServerAddressID();
		server.setSelection(serid);
		
		server.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				String selected = arg0.getItemAtPosition(arg2).toString();
				if (selected.equals("软工正式服务器"))
					ChocolateActivity.c_solver.setServerAddress(0);
				else
					ChocolateActivity.c_solver.setServerAddress(1);
				
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
			
		});
	}
	
	public void show() {
		ChocolateActivity.c_solver.setautoReflash(null,null);
		super.show();
	}

}
